package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;

import com.itheima.reggie.dto.DishDto;

import com.itheima.reggie.mapper.DishMapper;

import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;

import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

//连续的增删改需要看成一个整体,所以需要开启事务
@Transactional
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;


    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //将菜品信息存入菜品表
        //存入后菜品id会自动生成
        //菜品姓名重复会被全局异常抓住
        this.save(dishDto);

        //得到口味集合,遍历口味集合,将dishDto里菜品id存入口味表中
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));

        //将口味集合存入口味表
        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 修改之前先通过id查询数据
     *
     * @param id
     */
    @Override
    public DishDto getByIdWishFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();

        QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_id", dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);

        BeanUtils.copyProperties(dish, dishDto);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        QueryWrapper<DishFlavor> qw = new QueryWrapper<>();
        qw.eq("dish_id", dishDto.getId());
        dishFlavorService.remove(qw);
        //得到口味集
        //注意,这里同样是发送请求,携带的口味数据中依然没有dish_id;
        List<DishFlavor> flavors = dishDto.getFlavors();
        //设置id
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithFlavorByIds(List<Long> ids) {
        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.in("id", ids);
        List<Dish> list = this.list(dishQueryWrapper);
        for (Dish dish : list) {
            if (dish.getStatus() == 0) {
                //通过id删除dish表中数据
                this.removeById(dish.getId());
                //通过id删除dishFlavor表中数据
                dishFlavorService.removeById(dish.getId());
            }
        }
        for (Dish dish : list) {
            if (dish.getStatus() == 1) {
                throw new CustomerException("商品在售中");
            }
        }
    }
}
